package cn.yh.register.cmn.controller;

import cn.yh.register.cmn.service.DictService;
import cn.yh.register.common.result.Result;
import cn.yh.register.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//@CrossOrigin
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    DictService dictService;

    ////根据数据id查询子数据列表
    @GetMapping("/findChildData/{id}")
    @ApiOperation("根据id查询子数据")
    @ApiImplicitParam(name = "id",value = "id查询数据字典接口" ,required = true,dataType ="Long",paramType = "path")
    public Result<List<Dict>> findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    //导出数据
    @ApiOperation("导出数据")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    //导入数据功能
    @ApiOperation("导入数据")
    @PostMapping("/importData")
    public Result<?> importData(MultipartFile file){
        dictService.importData(file);
        return Result.ok();
    }

    @ApiOperation("获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(@PathVariable String value){
        //调用service
        return dictService.getDataByValueAndDictCode(null, value);
    }

    @ApiOperation("获取数据字典名称")
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,@PathVariable String value){
        //调用service
        return dictService.getDataByValueAndDictCode(dictCode,value);
    }

    @ApiOperation("根据字典编码查询子数据")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable String dictCode){
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return Result.ok(dictList);
    }


}
